<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

class PromotionController extends BaseController {

    public function index() {
        $this->display();
    }

    /**
     * 商品活动列表
     */
    public function actionGoodsList() {
        $parse_type = array('0' => '直接打折', '1' => '减价优惠', '2' => '固定金额出售', '3' => '买就赠优惠券');
        $level = M('user_level')->select();
        if ($level) {
            foreach ($level as $v) {
                $lv[$v['level_id']] = $v['level_name'];
            }
        }
        $this->assign("parse_type", $parse_type);

        $count = M('action_goods')->count();
        $Page = new \Think\Page($count, 10);
        $show = $Page->show();
        $res = M('action_goods')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        if ($res) {
            foreach ($res as $val) {
                if (!empty($val['group']) && !empty($lv)) {
                    $val['group'] = explode(',', $val['group']);
                    foreach ($val['group'] as $v) {
                        $val['group_name'] .= $lv[$v] . ',';
                    }
                }$rs [] = $val;
            }
        }

        $coupoRs = M('coupon')->where(array('type' => 0))->field('id,name')->select();
        $this->assign('couponList', $coupoRs);

        $this->assign('page', $show);
        $this->assign('prom_list', $rs);
        $this->display();
    }

    /**
     * 商品促销
     */
    public function actionGoodsInfo() {
        $this->assign('min_date', date('Y-m-d'));
        $level = M('user_level')->select();
        $this->assign('level', $level);
        $promId = I('id');
        $info['start_time'] = date('Y-m-d');
        $info['end_time'] = date('Y-m-d', time() + 3600 * 60 * 24);
        if ($promId > 0) {
            $info = M('action_goods')->where("id=$promId")->find();
            $info['group'] = explode(',', $info['group']);
            $info['start_time'] = date('Y-m-d', $info['start_time']);
            $info['end_time'] = date('Y-m-d', $info['end_time']);
            $rs = M('goods')->where("prom_id=$promId and prom_type=3")->select();
            $this->assign('action_goods', $rs);
        }

        $coupoRs = M('coupon')->where(array('type' => 0))->field('id,name')->select();
        $this->assign('couponList', $coupoRs);
        $this->assign('info', $info);
        $this->assign('min_date', date('Y-m-d'));
        $this->initEditor();
        $this->display();
    }

    public function actionGoodsSave() {
        $promId = I('id');
        $data = I('post.');
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if ($data['start_time'] >= $data['end_time']) {
            $this->error("时间设置有误");
            die();
        }
        $data['group'] = implode(',', $data['group']);
        if ($promId) {
            M('action_goods')->where("id=$promId")->save($data);
            $last_id = $promId;
            adminLog("管理员修改了商品促销 " . I('name'));
        } else {
            $last_id = M('action_goods')->add($data);
            adminLog("管理员添加了商品促销 " . I('name'));
        }

        if (is_array($data['goods_id'])) {
            $goodsId = implode(',', $data['goods_id']);
            if ($promId > 0) {
                M("goods")->where("prom_id=$promId and prom_type=3")->save(array('prom_id' => 0, 'prom_type' => 0));
            }
            M("goods")->where("goods_id in($goodsId)")->save(array('prom_id' => $last_id, 'prom_type' => 3));
        }
        $this->success('编辑促销活动成功', U('Promotion/actionGoodsList'));
    }

    public function actionGoodsDel() {
        $promId = I('id');
        $order_goods = M('order_goods')->where("prom_type = 3 and prom_id = $promId")->find();
        if (!empty($order_goods)) {
            $this->error("该活动有订单参与不能删除!");
        }
        M("goods")->where("prom_id=$promId and prom_type=3")->save(array('prom_id' => 0, 'prom_type' => 0));
        M('action_goods')->where("id=$promId")->delete();
        $this->success('删除活动成功', U('Promotion/actionGoodsList'));
    }

    /**
     * 活动列表
     */
    public function actionOrderList() {
        $parse_type = array('0' => '满额打折', '1' => '满额优惠金额', '2' => '满额送积分', '3' => '满额送优惠券');
        $level = M('user_level')->select();
        if ($level) {
            foreach ($level as $v) {
                $lv[$v['level_id']] = $v['level_name'];
            }
        }
        $count = M('action_order')->count();
        $Page = new \Think\Page($count, 10);
        $show = $Page->show();
        $res = M('action_order')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        if ($res) {
            foreach ($res as $val) {
                if (!empty($val['group']) && !empty($lv)) {
                    $val['group'] = explode(',', $val['group']);
                    foreach ($val['group'] as $v) {
                        $val['group_name'] .= $lv[$v] . ',';
                    }
                }$rs [] = $val;
            }
        }
        $this->assign('page', $show);
        $this->assign("parse_type", $parse_type);
        $this->assign('prom_list', $rs);
        $this->display();
    }

    /**
     * 订单促销
     */
    public function actionOrderInfo() {
        $this->assign('min_date', date('Y-m-d'));
        $level = M('user_level')->select();
        $this->assign('level', $level);
        $promId = I('id');
        $info['start_time'] = date('Y-m-d');
        $info['end_time'] = date('Y-m-d', time() + 3600 * 24 * 60);
        if ($promId > 0) {
            $info = M('action_order')->where("id=$promId")->find();
            $info['group'] = explode(',', $info['group']);
            $info['start_time'] = date('Y-m-d', $info['start_time']);
            $info['end_time'] = date('Y-m-d', $info['end_time']);
        }
        $coupoRs = M('coupon')->where(array('type' => 0))->field('id,name')->select();
        $this->assign('couponList', $coupoRs);
        $this->assign('info', $info);
        $this->assign('min_date', date('Y-m-d'));
        $this->initEditor();
        $this->display();
    }

    /**
     * 订单促销保存
     */
    public function actionOrderSave() {
        $promId = I('id');
        $data = I('post.');
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        $data['group'] = implode(',', $data['group']);
        if ($data['start_time'] >= $data['end_time']) {
            $this->error("时间设置有误");
            die();
        }
        if ($promId) {
            M('action_order')->where("id=$promId")->save($data);
            adminLog("管理员修改了商品促销 " . I('name'));
        } else {
            M('action_order')->add($data);
            adminLog("管理员添加了商品促销 " . I('name'));
        }
        $this->success('编辑促销活动成功', U('Promotion/actionOrderList'));
    }

    /**
     * 删除订单促销
     */
    public function actionOrderDel() {
        $promId = I('id');
        $order = M('order')->where("order_prom_id = $promId")->find();
        if (!empty($order)) {
            $this->error("该活动有订单参与不能删除!");
        }

        M('action_order')->where("id=$promId")->delete();
        $this->success('删除活动成功', U('Promotion/actionOrderList'));
    }

    /**
     * 团购列表
     */
    public function goodsGrouponList() {
        $Ad = M('goods_groupon');
        $count = $Ad->count();
        $Page = new \Think\Page($count, 10);
        $res = $Ad->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        if ($res) {
            foreach ($res as $val) {
                $val['start_time'] = date('Y-m-d H:i', $val['start_time']);
                $val['end_time'] = date('Y-m-d H:i', $val['end_time']);
                $list[] = $val;
            }
        }
        $this->assign('list', $list);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 团购
     */
    public function goodsGroupon() {
        $act = I('GET.act', 'add');
        $goodsGrouponId = I('get.id');
        $rs = array();
        $rs['start_time'] = date('Y-m-d');
        $rs['end_time'] = date('Y-m-d', time() + 3600 * 365);
        if ($goodsGrouponId) {
            $rs = D('goods_groupon')->where('id=' . $goodsGrouponId)->find();
            $rs['start_time'] = date('Y-m-d H:i', $rs['start_time']);
            $rs['end_time'] = date('Y-m-d H:i', $rs['end_time']);
            $act = 'edit';
        }
        $this->assign('min_date', date('Y-m-d'));
        $this->assign('info', $rs);
        $this->assign('act', $act);
        $this->display();
    }

    /**
     * 团购增册改
     */
    public function goodsGrouponHandle() {
        $data = I('post.');
        $data['goodsGroupon_intro'] = htmlspecialchars(stripslashes($_POST['goodsGroupon_intro']));
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if ($data['start_time'] >= $data['end_time']) {
            $this->error("时间设置有误");
            die();
        }
        if ($data['act'] == 'del') {
            $r = D('goods_groupon')->where('id=' . $data['id'])->delete();
            M('goods')->where("prom_type=2 and prom_id=" . $data['id'])->save(array('prom_id' => 0, 'prom_type' => 0));
            if ($r)
                exit(json_encode(1));
        }
        if ($data['act'] == 'add') {
            $r = D('goods_groupon')->add($data);
            M('goods')->where("goods_id=" . $data['goods_id'])->save(array('prom_id' => $r, 'prom_type' => 2));
        }
        if ($data['act'] == 'edit') {
            $r = D('goods_groupon')->where('id=' . $data['id'])->save($data);
            M('goods')->where("prom_type=2 and prom_id=" . $data['id'])->save(array('prom_id' => 0, 'prom_type' => 0));
            M('goods')->where("goods_id=" . $data['goods_id'])->save(array('prom_id' => $data['id'], 'prom_type' => 2));
        }
        if ($r) {
            $this->success("操作成功", U('Admin/Promotion/goodsGrouponList'));
        } else {
            $this->error("操作失败", U('Admin/Promotion/goodsGrouponList'));
        }
    }

    /**
     * 获取商品
     */
    public function getGoods() {
        $promId = I('id');
        $count = M('goods')->where("prom_id=$promId and prom_type=3")->count();
        $Page = new \Think\Page($count, 10);
        $goodsRs = M('goods')->where("prom_id=$promId and prom_type=3")->order('goods_id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('goodsList', $goodsRs);
        $this->display();
    }

    /**
     * 搜索商品
     */
    public function searchGoods() {
        $GoodsModel = new \Admin\Model\GoodsModel;
        $brandRs = $GoodsModel->getSortBrands();
        $this->assign('brandList', $brandRs);
        $categoryRs = $GoodsModel->getSortCategory();
        $this->assign('categoryList', $categoryRs);

        $goodsId = I('goods_id');
        $where = ' is_on_sale = 1 and prom_type=0 and store_count>0 ';
        if (!empty($goodsId)) {
            $where .= " and goods_id not in ($goodsId) ";
        }
        I('intro') && $where = "$where and " . I('intro') . " = 1";
        if (I('cat_id')) {
            $this->assign('cat_id', I('cat_id'));
            $grandsonIds = getGoodsSubCat(I('cat_id'));
            $where = " $where  and cat_id in(" . implode(',', $grandsonIds) . ") "; // 初始化搜索条件
        }
        if (I('brand_id')) {
            $this->assign('brand_id', I('brand_id'));
            $where = "$where and brand_id = " . I('brand_id');
        }
        if (!empty($_REQUEST['keywords'])) {
            $this->assign('keywords', I('keywords'));
            $where = "$where and (goods_name like '%" . I('keywords') . "%' or keywords like '%" . I('keywords') . "%')";
        }
        $count = M('goods')->where($where)->count();
        $Page = new \Think\Page($count, 10);
        $goodsRs = M('goods')->where($where)->order('goods_id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('goodsList', $goodsRs);
        $tpl = I('get.tpl', 'searchGoods');
        $this->display($tpl);
    }

    /**
     * 限时抢购
     */
    public function limitBuy() {
        $condition = array();
        $limitBuyModel = M('limit_buy');
        $count = $limitBuyModel->where($condition)->count();
        $Page = new \Think\Page($count, 10);
        $show = $Page->show();
        $rs = $limitBuyModel->where($condition)->order("id desc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('prom_list', $rs);
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 抢购增改
     */
    public function limitBuyInfo() {
        if (IS_POST) {
            $data = I('post.');
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            if ($data['start_time'] >= $data['end_time']) {
                $this->error("时间设置有误");
                die();
            }
            if (empty($data['id'])) {
                $r = M('limit_buy')->add($data);
                M('goods')->where("goods_id=" . $data['goods_id'])->save(array('prom_id' => $r, 'prom_type' => 1));
                adminLog("管理员添加抢购活动 " . $data['name']);
            } else {
                $r = M('limit_buy')->where("id=" . $data['id'])->save($data);
                M('goods')->where("prom_type=1 and prom_id=" . $data['id'])->save(array('prom_id' => 0, 'prom_type' => 0));
                M('goods')->where("goods_id=" . $data['goods_id'])->save(array('prom_id' => $data['id'], 'prom_type' => 1));
            }
            if ($r) {
                $this->success('编辑抢购活动成功', U('Promotion/limitBuy'));
                exit;
            } else {
                $this->error('编辑抢购活动失败', U('Promotion/limitBuy'));
            }
        }
        $id = I('id');
        $info['start_time'] = date('Y-m-d 00:00:00');
        $info['end_time'] = date('Y-m-d 23:59:59', time() + 3600 * 24 * 60);
        if ($id > 0) {
            $info = M('limit_buy')->where("id=$id")->find();
            $info['start_time'] = date('Y-m-d 00:00:00', $info['start_time']);
            $info['end_time'] = date('Y-m-d 23:59:59', $info['end_time']);
        }
        $this->assign('info', $info);
        $this->assign('min_date', date('Y-m-d'));
        $this->display();
    }

    /**
     * 删除抢购
     */
    public function limitBuyDel() {
        $id = I('del_id');
        if ($id) {
            M('limit_buy')->where("id=$id")->delete();
            M('goods')->where("prom_type=1 and prom_id=$id")->save(array('prom_id' => 0, 'prom_type' => 0));
            exit(json_encode(1));
        } else {
            exit(json_encode(0));
        }
    }

    /**
     * 拼团列表
     */
    public function fightGroupList() {
        $fightGroupMod = M('FightGroup');
        $count = $fightGroupMod->count();
        $Page = new \Think\Page($count, 10);
        $res = $fightGroupMod->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        if ($res) {
            foreach ($res as $val) {
                $val['start_time'] = date('Y-m-d H:i', $val['start_time']);
                $val['end_time'] = date('Y-m-d H:i', $val['end_time']);
                $list[] = $val;
            }
        }
        $this->assign('list', $list);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 添加拼团
     */
    public function fightGroup() {
        $act = I('GET.act', 'add');
        $id = I('get.id');
        $rs = array();
        $rs['start_time'] = date('Y-m-d');
        $rs['end_time'] = date('Y-m-d', time() + 3600 * 365);
        if ($id) {
            $rs = D('FightGroup')->where('id=' . $id)->find();
            $rs['start_time'] = date('Y-m-d H:i', $rs['start_time']);
            $rs['end_time'] = date('Y-m-d H:i', $rs['end_time']);
            $act = 'edit';
        }
        $this->assign('min_date', date('Y-m-d'));
        $this->assign('info', $rs);
        $this->assign('act', $act);
        $this->display();
    }

    /**
     * 拼团处理
     */
    public function fightGroupHandle() {
        $data = I('post.');
        $data['fightGroup_intro'] = htmlspecialchars(stripslashes($_POST['fightGroup_intro']));
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if ($data['act'] == 'del') {
            $r = D('FightGroup')->where('id=' . $data['id'])->delete();
            M('goods')->where("prom_type=4 and prom_id=" . $data['id'])->save(array('prom_id' => 0, 'prom_type' => 0));
            if ($r)
                exit(json_encode(1));
        }
        if ($data['act'] == 'add') {
            $r = D('FightGroup')->add($data);
            M('goods')->where("goods_id=" . $data['goods_id'])->save(array('prom_id' => $r, 'prom_type' => 4));
        }
        if ($data['act'] == 'edit') {
            $r = D('FightGroup')->where('id=' . $data['id'])->save($data);
            M('goods')->where("prom_type=4 and prom_id=" . $data['id'])->save(array('prom_id' => 0, 'prom_type' => 0));
            M('goods')->where("goods_id=" . $data['goods_id'])->save(array('prom_id' => $data['id'], 'prom_type' => 4));
        }
        if ($r) {
            $this->success("操作成功", U('Admin/Promotion/fightGroupList'));
        } else {
            $this->error("操作失败", U('Admin/Promotion/fightGroupList'));
        }
    }

    /**
     * 编辑器
     */
    private function initEditor() {
        $this->assign("URL_upload", U('Admin/Ueditor/imageUp', array('savepath' => 'promotion')));
        $this->assign("URL_fileUp", U('Admin/Ueditor/fileUp', array('savepath' => 'promotion')));
        $this->assign("URL_scrawlUp", U('Admin/Ueditor/scrawlUp', array('savepath' => 'promotion')));
        $this->assign("URL_getRemoteImage", U('Admin/Ueditor/getRemoteImage', array('savepath' => 'promotion')));
        $this->assign("URL_imageManager", U('Admin/Ueditor/imageManager', array('savepath' => 'promotion')));
        $this->assign("URL_imageUp", U('Admin/Ueditor/imageUp', array('savepath' => 'promotion')));
        $this->assign("URL_getMovie", U('Admin/Ueditor/getMovie', array('savepath' => 'promotion')));
        $this->assign("URL_Home", "");
    }

}
